//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#ifndef LOGICMANAGER_H_
#define LOGICMANAGER_H_

#include "ilink.h"
#include "rtexception.h"
#include "commandqueue.h"


class CLogicManager
{
public:
							CLogicManager(CLinkGuard& link, CCommandQueue& cmdQueue, class CMainWnd& wMain);
							~CLogicManager();

	void					Init(const std::string& strModemSettings, const std::string& strWirelessSettings);
	bool					Tick();
	void					ReportThreadExit(cExcReason reason=cERNone);

protected:
	void 					NotifyGUI( const COMMANDRESPONSE& cmdOut );
	void					DispatchCmd(const COMMANDREQUEST& cmd, COMMANDRESPONSE& cmdOut, bool& bNotify);


protected:
	void					OnGetAdslStats(COMMANDRESPONSE& cmdOut, bool& bNotify);
	void					OnExecuteModemXML(CMDSUBCODE subCode, const wxString& strScript, COMMANDRESPONSE& cmdOut, bool& bNotify);

	void					OnGetWirelessStat(COMMANDRESPONSE& cmdOut, bool& bNotify);
	void					OnExecuteWirelessXML(const wxString& strScript, COMMANDRESPONSE& cmdOut, bool& bNotify);


	//void					OnGetWirelessUsingProc(COMMANDRESPONSE& cmdOut, bool& bNotify);
	//void					OnGetWirelessUsingWL(COMMANDRESPONSE& cmdOut, bool& bNotify);

	void					ParseMACList(class CMarkupSTL& doc, const char* szTAG, const RTSTRING& strData);
	void					ParseStatus(class CMarkupSTL& doc, const char* szTAG, const RTSTRING& strData);
	wxString				BuildWirelessCMD();
	void					ExtractWirelessInt(const RTSTRING& strData);

public:
	//Parse helpers
	static PROCTABLEWITHHEADER ParseTableWithHeaders( const RTSTRING& strData, unsigned int nMinFielsInHeader=6, const char* szValTempl =" :\n\r\t"  );

	typedef bool (*FILTERPROC)(TABLEROW& row);
	static bool				IsDelimiter( const char c, const char* szTempl );
	static PROCTABLEWITHHEADER ParseTable( const RTSTRING& strData,FILTERPROC proc=NULL, const char* szValTempl =" :\n\r\t"  );

	static wxString			GeValFromTable(const PROCTABLEWITHHEADER& table, size_t stIDX, size_t tRow);
	static size_t			GeValFromTableInt(const PROCTABLEWITHHEADER& table, size_t stIDX, size_t tRow);

	static wxString			GeValFromTable(const PROCTABLEWITHHEADER& table, const char* szHeader, size_t tRow, size_t tHeaderNameDupIDX );
	static size_t			GeValFromTableInt(const PROCTABLEWITHHEADER& table, const char* szHeader, size_t tRow, size_t tHeaderNameDupIDX );
	static long				ToNumberValIfLineHave( const RTSTRING& strFile,  const RTSTRING& strPattern);
	static long				ToNumberVal( const RTSTRING& strVal );
	static RTSTRING			GetStringWithPattern(const RTSTRING& strData,  const RTSTRING& strPattern, bool bReturnWitoutPattern = false);
	static RTSTRING			GetNextStringWithPattern(const RTSTRING& strData,  const RTSTRING& strPattern);
	static size_t			StringToDigit(const std::string& strVal, unsigned long long vals[], size_t tMax );
	static bool				ExtractPairs( const char* szDelim, const RTSTRING& strData, NAMEDVALS& lst, const char* szStartFrom=NULL ); 	 

	typedef std::vector<unsigned char> INTLIST;
	typedef unsigned char (*transform_fn)(const char* szDigit);

	static INTLIST			ParseDataTable(const RTSTRING& strData, const char* szTableStart, transform_fn f);
	static void				GetSection(const std::string& strStatData, const char* szPattern, NAMEDVALS& lstRes);

	
private:
	CLinkGuard& 			m_link;
	CCommandQueue& 			m_cmdQueue;
	class CMainWnd& 		m_wMain;
	class CADSLModem*		m_pModem;
	class CWirelessHW*		m_pAccessPoint;
	class CShellCmdProcessor* m_pProcCMD;
};

#endif /*LOGICMANAGER_H_*/
